<template>
    <div class="demo">
        <h1>当前和为: {{ sum }}</h1>
        <select v-model.number="count">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
        <button @click="add">+</button>
        <button @click="sub">-</button>
        <button @click="addWhenOdd">WhenOdd</button>
        <button @click="addWaitAMonent">WaitAMonent</button>
    </div>
</template>

<script>
export default {
    name: 'CountComponent',
    data() {
        return {
            sum: 0,
            count: 1
        };
    },
    methods: {
        add() {
            this.sum += this.count;
        },
        sub() {
            this.sum -= this.count;
        },
        addWhenOdd() {
            if (this.sum % 2 !== 0) {
                this.sum += this.count;
            }
        },
        addWaitAMonent() {
            setTimeout(() => {
                this.sum += this.count;
            }, 1000);
        }
    }
};
</script>
<style scoped>
    button {
        margin-left: 10px;
    }
</style>